package com.kaiqingsun.io;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.Executors;

public class Demo1 {
    public static void main(String[] args) throws IOException {
        //1.绝对路径的方式创建
        //1.1直接传入字符串的路径
        File file1 = new File("D:\\比特\\t图片");
        //1.2传入父路径 + 子路径
        File file2 = new File("D:\\比特","t图片");
        //1.3以File传入parent
        File parent = new File("D:\\比特");
        File file3 = new File(parent,"t图片");
        //负数代表file1《file2，0代表相等
        int cmp1 = file1.compareTo(file2);
        int cmp2 = file2.compareTo(file3);
        System.out.println(cmp1);
        System.out.println(cmp2);

        File file4 = new File("D:\\比特\\图片");
        //异常的知识点：JAVA中的异常分为受查异常（checked exception） or 非受查异常
        //方法中如果可能抛出受查异常， 有语法要求
        //要么我们自己try catch 不在抛出受查一唱歌要么在方法签名中通过throws声明有受查异常可能被抛出
        //java.long.RuntimeException 及其子类是非受查异常
        // java.io。IOException 是受查异常
        boolean r1 = file4.createNewFile();
        System.out.println(r1);
        r1 = file4.createNewFile();
        System.out.println(r1);

        System.out.println(file1.exists());
        System.out.println(file1.isDirectory());
        System.out.println(file1.isFile());
        File[] files = File.listRoots();
        System.out.println(Arrays.toString(files));

        }


    //1.深度优先的遍历
    private static  void traversalDepthFirst(File dir) throws Exception {
        //1.找到这个子目录下的所有孩子
        File[] files= dir.listFiles();
        if (files == null){
            return;
        }
        //针对每个孩子，判断是目录还是文件
        for (File file : files){
            System.out.println("[D]"+file.getCanonicalPath());
            if (file.isDirectory()){
                //r如果确定是个目录，则继续递归去遍历处理
                traversalDepthFirst(file);
            }else {
                System.out.println("[F]"+file.getCanonicalPath());
            }
        }
    }
    //广度优先的遍历
    private static void traversalBoradFirst(File dir) throws Exception{
        Queue<File> queue = new LinkedList<>();
        queue.offer(dir);

        while (!queue.isEmpty()){
            File poll = queue.poll();
            if (poll.isDirectory()){
                System.out.println("[D]"+poll.getCanonicalPath());
            }else {
                System.out.println("[F]"+poll.getCanonicalPath());
            }
            if (poll.isDirectory()){
                File[] files = poll.listFiles();
                if(files == null){
                    continue;
                }
                for (File file  :files){
                    queue.offer(file);
                }
            }
        }

    }
}
